package org.spring.blog.controller.portal;

import org.spring.blog.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/portal/image")
public class ImagePortalApi {

    @Autowired
    IImageService imageService;

    /**
     * 查找图片
     *
     * @param _url 图片id
     */
    @GetMapping("/{url}")
    public void query(@PathVariable("url") String _url,
                      HttpServletResponse _response) {
        try {
            imageService.query(_url, _response);
        } catch (IOException _e) {
            _e.printStackTrace();
        }
    }

    /**
     * 返回二维码的方法
     *
     * @param code 二维码对应的id
     */
    @GetMapping("/qr-code/{code}")
    public void getQrCodeImage(@PathVariable String code,
                               HttpServletResponse _response,
                               HttpServletRequest _request) {
        imageService.getQrCodeImage(code, _response, _request);
    }
}
